package com.yjhb.android.sbz.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.Task;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.tool.ZxToast;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.tencent.qcloud.tim.uikit.component.action.PopDialogAdapter;
import com.tencent.qcloud.tim.uikit.component.action.PopMenuAction;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListAdapter;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListLayout;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationManagerKit;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationProvider;
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/5 16:27
 */
public class MsgActivity extends BaseActivity implements ConversationListLayout.OnItemClickListener, ConversationListLayout.OnItemLongClickListener, IUIKitCallBack {
    private final String TAG = "MsgActivity";

    @BindView(R.id.conversationListLayout)
    ConversationListLayout conversationListLayout;
    @BindView(R.id.title_tv)
    TextView title_tv;

    ConversationListAdapter adapter;

    private int type = 2; // 1选择联系人发送任务 2正常聊天列表
    private Task task;

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_msg;
    }

    @Override
    protected void initUI() {
        type = getIntent().getIntExtra("type", 2);
        if (type == 1) {
            task = (Task) getIntent().getSerializableExtra("task");
            title_tv.setText("选择您要分享的联系人");
        } else {
            title_tv.setText("消息");
        }
        conversationListLayout.setAdapter(adapter = new ConversationListAdapter());
    }

    @Override
    protected void initData() {
        ConversationManagerKit.getInstance().loadConversation(this);
    }

    @Override
    protected void regEvent() {
        adapter.setOnItemClickListener(this);
        adapter.setOnItemLongClickListener(this);
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @Override
    public void onItemClick(View view, int position, ConversationInfo messageInfo) {
        if (type == 1) {
            Tool.sendTaskMsg(this,messageInfo.getId(), task, type);
            ZxToast.showSuccessBottomToast("分享成功！");
            finish();
        } else {
            Tool.startChatActivity(this, messageInfo.getId(), messageInfo.getTitle(), 0);
        }
    }


    @Override
    public void OnItemLongClick(View view, int position, ConversationInfo messageInfo) {
        PopDialogAdapter adapter = null;
        QMUIListPopup popup = new QMUIListPopup(this, QMUIPopup.DIRECTION_TOP, adapter = new PopDialogAdapter());
        popup.create(100 * ResManager.dp(), -2, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position2, long id) {
                if (position2 == 0) {
                    ConversationManagerKit.getInstance().deleteConversation(position, messageInfo);
                    popup.dismiss();
                }
            }
        });
        List<PopMenuAction> actions = new ArrayList<>();
        PopMenuAction action = new PopMenuAction();
        action.setActionName("刪除");
        actions.add(action);
        adapter.setDataSource(actions);
        popup.show(view);
    }

    @Override
    public void onSuccess(Object data) {
        adapter.setDataProvider((ConversationProvider) data);
    }

    @Override
    public void onError(String module, int errCode, String errMsg) {

    }
}
